import {
  WindowsBalloon
} from "node-notifier";

/* eslint-disable */

//所有的监听器
var propertiesListener;
var goSettingListener;
var statuBarHeightListener;
var requestPropertyListener;
var sendPropertyListener;
var sendPropertiesListener;
var deviceInfoListener;
var devInfoAllListener;
var devCacheAllListener;
var devCacheListener;
var onResumeListener;
var onPauseListener;

var createRuleListener;
var deleteRuleListener;
var getRulesListener;
var controlRuleActiveListener;

var jumpToSettingListener;

var requestSwitchPropertyListener;
var requestControlPropertyListener;
var requestStatusPropertyListener;
var requestGearPropertyListener;
var requestWarnEventPropertyListener;
var requestFaultEventPropertyListener;

//保存/获取/删除数据库中的键值对
var saveStorageSyncListener; //保存
var getStorageSyncListener; //获取
var removeStorageSyncListener; //删除



/**
 * 下发单指令(向设备发送单个控制指令)
 * @param {object} jsObject：指令对象,js对象。例如：{switch: {on: 1}} 其中switch是 sid(服务id), on是 pid(属性id)
 */
function sendProperty(jsObject) {

  // let json_data = JSON.stringify({ switch: { on: 1 } });//将js值转换成json字符串
  let json_data = JSON.stringify(jsObject); //将js对象转换成json字符串
  console.log(getCurrentTime() + ' 开始发送指令: ' + json_data);
  // toast(2500, json_data);
  if (typeof hilink != "undefined") {
    // console.log('-------------hilink send property--------------');
    hilink.setDeviceInfo("0", json_data, "sendPropertyCallback");
  } else {
    console.log('-------------hilink is null--------------');
  }
}
/**
 * 下发指令(向设备发送控制指令)的回调函数
 * @param {json格式的string} res : 发送结果
 */
window.sendPropertyCallback = res => {
  // toast(2500, res);
  let data = dataChange(res);
  if (sendPropertyListener != null) {
    sendPropertyListener(data);
  }
  // console.log('--------------------sendPropertyCallback: result = ' + data);
}

function setSendPropertyListener(listener) {
  sendPropertyListener = listener;
}

/**
 * 下发自带回调的单指令(向设备发送单个控制指令)
 * @param {object} jsObject：指令对象,js对象。例如：{switch: {on: 1}} 其中switch是 sid(服务id), on是 pid(属性id)
 */
function sendPropertyWithCallback(jsObject, sendPropertyCallback) {

  // let json_data = JSON.stringify({ switch: { on: 1 } });//将js值转换成json字符串
  let json_data = JSON.stringify(jsObject); //将js对象转换成json字符串
  console.log(getCurrentTime() + ' 开始发送自带回调的指令: ' + json_data);
  // toast(2500, json_data);
  if (typeof hilink != "undefined") {
    // console.log('-------------hilink send property--------------');
    hilink.setDeviceInfo("0", json_data, sendPropertyCallback);
  } else {
    console.log('-------------hilink is null--------------');
  }
}

/**
 * 下发多指令（向设备发送多条控制指令）
 * @param {*} jsObject
 */
function sendProperties(jsObject) {
  let json_data = JSON.stringify(jsObject); //将js对象转换成json字符串
  console.log(getCurrentTime() + ' 开始发送多指令: ' + json_data);
  if (typeof hilink != "undefined") {
    hilink.setMultipleDeviceInfo(
      "0", //devid,string,设备id，若取"0"，表示当前设备
      json_data, //body,string,控制设备的命令，见下描述
      "true", //boolean, true 并行下发，false 串行下发
      "true", //boolean, 是否只要有一个命令成功就会回调返回结果
      "sendPropertiesCallback", //resultCallback，成功或失败时，将调用传入resultStr返回结果
    );
  } else {
    console.log('-------------hilink is null--------------');
  }

}
/**
 * 下发多指令(向设备发送多个控制指令)的回调函数
 * @param {json格式的string} res : 发送结果
 */
window.sendPropertiesCallback = res => {
  let data = dataChange(res);
  if (sendPropertiesListener != null) {
    sendPropertiesListener(data);
  }
}

function setSendPropertiesListener(listener) {
  sendPropertiesListener = listener;
}

/**
 * 上报单个属性(接收设备上传的属性)
 * @param {*} sid: 服务id
 * @param {*} pid: 属性id
 */
function getProperty(sid, pid) {
  // let sidStr = JSON.stringify(sid);//将js对象转换成json字符串
  // let pidStr = JSON.stringify(pid);//将js对象转换成json字符串
  if (typeof hilink != "undefined") {
    hilink.getDevInfo(
      "0", //devid string,请取"0"，表示当前设备，暂不支持其它取值
      sid, // sid,string, 为该设备的service id，取值参见HiLink Profile定义
      pid, //body,string,可选，见下描述
      "getPropertyCallback" //resultCallback，成功或失败时，将调用传入resultStr返回结果
    ); //web页面中需实现该函数，用来获取执行结果
  } else {
    console.log('-------------hilink is null--------------');
    // var json = '{"devId":"{deviceId}", "gatewayId":"xxxxx", "services": [{ "st": "air_conditioner","ts": "20151212T121212Z","sid": "1", "data": { "characteristicName1": "value1","characteristicName2": "value2"}} ]}';
    // var obj = JSON.parse(json);
    // console.log(obj.services[0].data.characteristicName1);
  }
}
window.getPropertyCallback = resultStr => {
  let data = dataChange(resultStr);
  if (requestPropertyListener != null) {
    requestPropertyListener(data);
  }
  // propertyCallback(result);
}

function setRequestPropertyListener(listener) {
  requestPropertyListener = listener;
}


/**
 * 获取switch服务下的所有属性数据
 */
function getSwitchProperties() {
  if (typeof hilink != "undefined") {
    hilink.getDevInfo(
      "0", //devid string,请取"0"，表示当前设备，暂不支持其它取值
      "switch", // sid,string, 为该设备的service id，取值参见HiLink Profile定义
      "", //body,string,可选，见下描述
      "getSwitchPropertiesCallback" //resultCallback，成功或失败时，将调用传入resultStr返回结果
    ); //web页面中需实现该函数，用来获取执行结果
  }
}
window.getSwitchPropertiesCallback = resultStr => {
  let data = dataChange(resultStr);
  if (requestSwitchPropertyListener != null) {
    requestSwitchPropertyListener(data);
  }
}

function setRequestSwitchPropertiesListener(listener) {
  requestSwitchPropertyListener = listener;
}

/**
 * 获取control服务下的所有属性数据
 */
function getControlProperties() {
  if (typeof hilink != "undefined") {
    hilink.getDevInfo(
      "0", //devid string,请取"0"，表示当前设备，暂不支持其它取值
      "control", // sid,string, 为该设备的service id，取值参见HiLink Profile定义
      "", //body,string,可选，见下描述
      "getControlPropertiesCallback" //resultCallback，成功或失败时，将调用传入resultStr返回结果
    ); //web页面中需实现该函数，用来获取执行结果
  }
}
window.getControlPropertiesCallback = resultStr => {
  let data = dataChange(resultStr);
  if (requestControlPropertyListener != null) {
    requestControlPropertyListener(data);
  }
}

function setRequestControlPropertiesListener(listener) {
  requestControlPropertyListener = listener;
}

/**
 * 获取status服务下的所有属性数据
 */
function getStatusProperties() {
  if (typeof hilink != "undefined") {
    hilink.getDevInfo(
      "0", //devid string,请取"0"，表示当前设备，暂不支持其它取值
      "status", // sid,string, 为该设备的service id，取值参见HiLink Profile定义
      "", //body,string,可选，见下描述
      "getStatusPropertiesCallback" //resultCallback，成功或失败时，将调用传入resultStr返回结果
    ); //web页面中需实现该函数，用来获取执行结果
  }
}
window.getStatusPropertiesCallback = resultStr => {
  let data = dataChange(resultStr);
  if (requestStatusPropertyListener != null) {
    requestStatusPropertyListener(data);
  }
}

function setRequestStatusPropertiesListener(listener) {
  requestStatusPropertyListener = listener;
}

/**
 * 获取gear服务下的所有属性数据
 */
function getGearProperties() {
  if (typeof hilink != "undefined") {
    hilink.getDevInfo(
      "0", //devid string,请取"0"，表示当前设备，暂不支持其它取值
      "gear", // sid,string, 为该设备的service id，取值参见HiLink Profile定义
      "", //body,string,可选，见下描述
      "getGearPropertiesCallback" //resultCallback，成功或失败时，将调用传入resultStr返回结果
    ); //web页面中需实现该函数，用来获取执行结果
  }
}
window.getGearPropertiesCallback = resultStr => {
  let data = dataChange(resultStr);
  if (requestGearPropertyListener != null) {
    requestGearPropertyListener(data);
  }
}

function setRequestGearPropertiesListener(listener) {
  requestGearPropertyListener = listener;
}

/**
 * 获取WarnEvent服务下的所有属性数据
 */
function getWarnEventProperties() {
  if (typeof hilink != "undefined") {
    hilink.getDevInfo(
      "0", //devid string,请取"0"，表示当前设备，暂不支持其它取值
      "WarnEvent", // sid,string, 为该设备的service id，取值参见HiLink Profile定义
      "", //body,string,可选，见下描述
      "getWarnEventPropertiesCallback" //resultCallback，成功或失败时，将调用传入resultStr返回结果
    ); //web页面中需实现该函数，用来获取执行结果
  }
}
window.getWarnEventPropertiesCallback = resultStr => {
  let data = dataChange(resultStr);
  if (requestWarnEventPropertyListener != null) {
    requestWarnEventPropertyListener(data);
  }
}

function setRequestWarnEventPropertiesListener(listener) {
  requestWarnEventPropertyListener = listener;
}

/**
 * 获取faultEvent服务下的所有属性数据
 */
function getFaultEventProperties() {
  if (typeof hilink != "undefined") {
    hilink.getDevInfo(
      "0", //devid string,请取"0"，表示当前设备，暂不支持其它取值
      "faultEvent", // sid,string, 为该设备的service id，取值参见HiLink Profile定义
      "", //body,string,可选，见下描述
      "getFaultEventPropertiesCallback" //resultCallback，成功或失败时，将调用传入resultStr返回结果
    ); //web页面中需实现该函数，用来获取执行结果
  }
}
window.getFaultEventPropertiesCallback = resultStr => {
  let data = dataChange(resultStr);
  if (requestFaultEventPropertyListener != null) {
    requestFaultEventPropertyListener(data);
  }
}

function setRequestFaultEventPropertiesListener(listener) {
  requestFaultEventPropertyListener = listener;
}


/**
 * 该方法监听设备上报的数据(事件)
 * @param {json格式的string} res 上报的数据
 */
window.deviceEventCallback = res => {
  console.log(getCurrentTime(), ' 设备上报的数据：', res);
  let data = dataChange(res);
  if (propertiesListener != null) {
    propertiesListener(data);
  }
}
// 设置数据上报事件监听器(必须先设置才能收到上报事件)
function setPropertiesListener(listener) {
  propertiesListener = listener;
}

/**
 * 获取设备当前所有状态的缓存数据
 */
function getDevCacheAll() {
  if (window.hilink) {
    //devid: string, 请取"0"，表示当前设备，暂不支持其它取值
    //body: string,可选，body中可选的列举出需要获取的设备属性id。例如：{ "characteristicName1", "characteristicName2" }
    //devCacheAllCallback: 成功或失败时，将调用传入res返回结果
    window.hilink.getDevCacheAll('0', '', 'devCacheAllCallback');
  }
}
/**
 * 获取设备当前所有状态的缓存数据的回调函数
 * @param {json格式的string} res 返回的结果
 */
window.devCacheAllCallback = res => {
  let data = dataChange(res);
  console.log('获取设备缓存全集：', data);
  //更新UI
  if (devCacheAllListener != null) {
    devCacheAllListener(data);
  }
}

function setDevCacheAllListener(listener) {
  devCacheAllListener = listener;
}

/**
 * APP从后台恢复时会回调该函数
 */
window.onResume = () => {
  console.log('APP从后台恢复到设备页了')
}

/**
 * 解析数据
 * @param {json格式的string} res 收到的数据
 * @returns 返回解析过的数据
 */
function dataChange(res) {
  let data = undefined;
  let dataStr = res;
  dataStr = dataStr.replace(/"{/g, '{');
  dataStr = dataStr.replace(/}"/g, '}');
  dataStr = dataStr.replace(/\\|\n|\r|\t|\f|\t/g, '');
  data = JSON.parse(dataStr);
  return data;
}

// // app下发数据给设备
// statusRight.addEventListener('click', function (event) {
//   let switchvalue;
//   let devReport;   // 模拟设备上报数据

//   if (switchStatus) {
//     switchvalue = 0;
//   } else {
//     switchvalue = 1;
//   }

//   let json_data = JSON.stringify({ switch: { on: switchvalue } });

//   console.log('APP下发:', json_data)

//   if (typeof hilink != "undefined") {
//     hilink.setDeviceInfo("0", json_data, "switchResult");
//   } else { // 本地模拟指令下发成功
//     if (switchvalue) {
//       switchStatus = true;
//       devReport = '{"sid":"switch","data":{"on":1}}';
//     } else {
//       switchStatus = false;
//       devReport = '{"sid":"switch","data":{"on":0}}';
//     }
//     // 模拟接收设备上报，自动触发 deviceEventCallback()
//     deviceEventCallback(devReport);
//   }
//   event.stopPropagation();
// })

// // 退出当前设备页，返回APP设备列表页
// barLeft.addEventListener('click', function (event) {
//   if (window.hilink) {
//     hilink.finishDeviceActivity();
//   }
//   event.stopPropagation();
// })

/**
 * 获取手机状态栏高度
 */
function getStatusBarHeight() {
  if (window.hilink) {
    hilink.getStatusBarHeight('barHeightRes');

  }
}
window.barHeightRes = (res) => {
  let height = dataChange(res).statusBarHeight;
  console.log('手机状态栏高度:', height);
  if (statuBarHeightListener != null) {
    statuBarHeightListener(height);
  }
  // setStatusBarHeight(height);
}

function setStatuBarHeightListener(listener) {
  statuBarHeightListener = listener;
}
/**
 * 设置状态栏高度
 * @param {number} num
 */
function setStatusBarHeight(num) {
  iphoneStatus.style.height = num + 'px';
  devTop.style.height = (58 + num) + 'px';
}

function getAppLanguage() {
  if (typeof hilink != "undefined") {
    return hilink.getAppLanguageSync();
  }
  return "zh";
}

// // 判断当前手机语言
// function getAppLanguage() {
//   let language = undefined;
//   let reg = /^zh-/i;
//   let DEFAULT_LANGUAGE = 'zh-CN';

//   if (window.hilink && window.hilink.getAppLanguageSync) {
//     language = window.hilink.getAppLanguageSync().toLowerCase();
//   } else if (navigator && navigator.language) {
//     language = navigator.language.toLowerCase();
//   } else {
//     language = DEFAULT_LANGUAGE;
//   }

//   let test = reg.test(language);

//   if (test) {
//     language = 'zh-CN';
//   } else if (language === 'bo-cn') { //藏语处理
//     language = 'zh-CN';
//   } else { //非中文或藏语，则均默认为英文
//     language = 'en-UK';
//   }
//   console.log('当前语言:', language);
//   return language;
// }

/**
 * 设置标题栏是否可见
 * @param {boolean} visible  标题栏是否可见
 */
function setNavigationVisible(visible) {
  if (typeof hilink != "undefined") {
    hilink.setTitleVisible(
      visible, //URI,boolean, true，标题可见，false标题不可见
      "navigationVisibleCallback" //resultCallback，成功或失败时，将调用传入resultStr返回结果
    );
  }
}
window.navigationVisibleCallback = res => {
  // console.log('---------------navigationVisibleCallback: result = ' + res);
}

/**
 * 修改标题栏的样式
 */
function modifyTitleBar(isTextLightColor) {
  if (window.hilink) {
    window.hilink.modifyTitleBar(isTextLightColor, //boolean, true，标题栏及状态栏文字白色，false标题栏及状态栏文字黑色
      "#FFFFEE00", //string, ARGB背景色值，目前已无作用
      "modifyTitleBarCallback" //resultCallback，成功或失败时，将调用传入resultStr返回结果
    );
  }
}
window.modifyTitleBarCallback = res => {
  // console.log("-----------------modifyTitleBarCallback res = " + res);
  // toast("3000", "modifyTitleBar:" + res);
}
/**
 * 设置提示信息是否可见
 */
function setMessageVisible() {
  if (window.hilink) {
    hilink.setMessageVisible(false, //URI,boolean, true，提示信息可见，false提示信息不可见
      "messageVisibleCallback" //messageVisibleCallback，成功或失败时，将调用传入res返回结果
    );
  }
}
window.messageVisibleCallback = res => {
  console.log("-----------------messageVisibleCallback res = " + res);
}

/**
 * 修改标题样式
 * //int，标题样式值，0，1为老版本样式，不支持通过该接口变更，2为Emui 10.0风格标题，后续可扩展
 */
function setChangeTitleStyle() {
  if (window.hilink) {
    window.hilink.changeTitleStyle(2);
  }
}
/**
 * 是否重写标题栏右侧按钮点击事件
 * @param {boolean} enable 是否启用重写点击事件
 * @param {string} callback，成功或失败时，将调用传入resultStr返回结果
 */
function overrideTitleBarRightBtn(enable) {
  if (window.hilink) {
    hilink.overrideSettingClick(enable, "clickTitleBarRightBtn");

  }
}
/**
 * 重写标题栏右侧按钮点击事件的回调函数
 * @param {string} res
 */
window.clickTitleBarRightBtn = res => {
  console.log(res); //返回成功与否
}
/**
 * 重写标题栏右侧按钮事件启用成功后，会自动调用这个方法实现页面跳转
 */
window.goSetting = () => {
  //实现“设置”按钮的跳转事件
  if (goSettingListener != null) {
    goSettingListener();
  }

}

function setgoSettingListener(listener) {
  goSettingListener = listener;
}

/**
 * 弹出原生的提示框
 * @param {string} time 提示显示时长
 * @param {string} content 提示内容
 */
function toast(time, content) {
  if (window.hilink) {
    window.hilink.toast(time, content);
  }
}
/**
 * 添加覆盖提示
 * @param {string} jsonMsgBody  列举出需要添加的覆盖提示的的属性。
 * {
 * "type": 24 //自定义int值
 * "key": 1  //目前请均传值1
 * "icon": 1 //覆盖消息前图标的编号，可以为Null，可传值为数组对应角标，参考下图
 * "color": 0 //字体颜色编号，可以为null，可传值为数组对应角标，可传值见下图
 * "msg": "ssssss" //需要显示的信息
 * }
 * @param {string} addMessageCallback 添加提示成功或失败时的回调函数，将调用传入res返回结果
 */
// function addMessage(jsonMsgBody, addMessageCallback) {
//   if (window.hilink) {
//     hilink.addMessage("json_body",	//body,string，见下描述
//       "addMessageCallback"  //addMessageCallback，
//     );
//   }
// }
// window.addMessageCallback = res => {
//   console.log('---------------addMessageCallback:', res);
// }

/**
 * 退出设备页面
 */
function finishDeviceActivity() {
  if (window.hilink) {
    window.hilink.finishDeviceActivity();
  }
}
/**
 * 打印debug级别的日志
 * @param {boolean} isSaveLog 是否保存日志信息
 * @param {string} tag 日志信息的关键字
 * @param {string} msg 日志的内容
 */
function debugLog(isSaveLog, tag, msg) {
  if (window.hilink) {
    window.hilink.printLogDebug(true, tag, msg);
  }
}
/**
 * 打印用户级别的日志
 * @param {boolean} isSaveLog 是否保存日志信息
 * @param {string} tag 日志信息的关键字
 * @param {string} msg 日志的内容
 */
function userLog(isSaveLog, tag, msg) {
  if (window.hilink) {
    window.hilink.printLog(true, tag, msg);
  }
}

/**
 * 刷新整个h5页面
 */
function refreshWebView() {
  if (window.hilink) {
    window.hilink.refreshWebView();
  }
}

/**
 * 获取暗黑模式(夜晚模式)
 * 1：dark模式 2：正常模式
 */
function getDarkMode() {
  if (window.hilink) {
    var isDarkMode = window.hilink.getDarkMode();
    return isDarkMode;
  }
  return 1;
}
/**
 * 删除设备
 */
function deleteDevice() {
  if (window.hilink) {
    hilink.deleteDevice(
      true, // isAssociate,boolean, 是否删除设备下挂关联设备，目前只有桥设备用到
      "deleteDeviceCallback" //resultCallback，成功或失败时，将调用传入resultStr返回结果
    );
  }
}
window.deleteDeviceCallback = res => {
  console.log("-----------------deleteDeviceCallback res = " + res);
  if (deleteDevicelistener != null) {
    deleteDevicelistener(res);
  }
}
var deleteDevicelistener;

function setDeleteDeviceCallBack(callback) {
  deleteDevicelistener = callback;
}

/**
 * 修改本设备名称
 * @param {string}} deviceName
 */
function modifyDeviceName(deviceName) {
  if (window.hilink) {
    hilink.modifyDeviceName(
      deviceName, // deviceName,String,修改的设备名称字段
      "modifyDeviceNameCallback" //resultCallback，成功或失败时，将调用传入resultStr返回结果
    );
  }
}
window.modifyDeviceNameCallback = res => {
  console.log("-----------------modifyDeviceNameCallback res = " + res);
  if (modifyDeviceNameListener != null) {
    modifyDeviceNameListener(res);
  }
}
var modifyDeviceNameListener;

function setModifyDeviceNameCallBack(callback) {
  modifyDeviceNameListener = callback;
}

/**
 * 修改设备名称通过id
 * @param {string} deviceName
 * @param {string} deviceId
 */
function modifyDeviceNameById(deviceName, deviceId) {
  if (window.hilink) {
    hilink.modifyDeviceNameByDevId(
      deviceId, // deviceId,String,修改的设备id
      deviceName, // deviceName,String,修改的设备名称字段
      "modifyDeviceNameByIdCallback" //resultCallback，成功或失败时，将调用传入resultStr返回结果
    );
  }
}
window.modifyDeviceNameByIdCallback = res => {
  console.log("-----------------modifyDeviceNameByIdCallback res = " + res);
  if (modifyDeviceNameByIdListener != null) {
    modifyDeviceNameByIdListener(res);
  }
}
var modifyDeviceNameByIdListener;

function setModifyDeviceNameByIdCallBack(callback) {
  modifyDeviceNameByIdListener = callback;
}

/**
 * 修改设备摆放房间
 * @param {string} roomName
 */
function modifyRoomName(roomName) {
  if (window.hilink) {
    hilink.modifyDeviceRoomName(
      "卫生间", // chooseRoomString,String, 修改的设备摆放位置信息的字段
      "modifyDeviceRoomNameCallback" //resultCallback，成功或失败时，将调用传入resultStr返回结果
    );
  }
}
window.modifyDeviceRoomNameCallback = res => {
  if (modifyRoomListener != null) {
    modifyRoomListener(res);
  }
}
var modifyRoomListener;

function setModifyRoomCallBack(callback) {
  modifyRoomListener = callback;
}

/**
 * 获取当前设备的信息(有缓存，第二次请求还是第一次的数据，除非退出扩展程序)
 */
function getDeviceInfo() {
  if (window.hilink) {
    hilink.getDeviceInfo(
      "getDeviceInfoCallback" //resultCallback，成功或失败时，将调用传入resultStr返回结果
    );
  }
}
window.getDeviceInfoCallback = res => {
  let data = dataChange(res);
  if (deviceInfoListener != null) {
    deviceInfoListener(data);
  }
  /*
  {
    "deviceId": "479******************************443",
    "devName": "摇控器(8)",
    "roomName": "null",
    "status": "online",
    "gatewayId": "a67******************************ec2",
    "nodeType": "ENDPOINT",
    "devInfo": {
      "sn": "@LH**************168",
      "manu": "021",
      "devType": "038",
      "model": "B910ZB",
      "mac": "00:***********:68",
      "hiv": "1.0.5",
      "fwv": "1.0.1",
      "hwv": "v01.3",
      "swv": "b00.2",
      "id": "1015",
      "prototype": "1"
    },
    "services": [
      {
        "sid": "keyEvent",
        "st": "keyEvent",
        "ts": "20171225T074217Z",
        "data": {
          "key": 0
        }
      },
      …
    ]
  }

  */
}

function setDeviceInfoListener(listener) {
  deviceInfoListener = listener;
}

/**
 * 获取当前设备所有的信息(不是缓存数据，每次请求都是最新的)
 *
 */
function getDevInfoAll() {
  if (window.hilink) {
    hilink.getDevInfoAll(
      "0", //devId(String): 华为云分配给设备的唯一id,获取本设备时传0。
      "", //body(String): 该参数为预留参数，当前请传空值。
      "getDevInfoAllCallback" //resultCallback，回调方法，成功或失败时，将调用传入resultStr返回结果
    );
  }
}
window.getDevInfoAllCallback = res => {
  let data = dataChange(res);
  if (devInfoAllListener != null) {
    devInfoAllListener(data);
  }
  /*
  {
    "devId": "479******************************443",
    "gatewayId": "a67******************************ec2",
    "nodeType": "ENDPOINT",
    "devInfo": {
      "sn": "@LH**************168",
      "manu": "021",
      "devType": "038",
      "model": "B910ZB",
      "mac": "00:***********:68",
      "hiv": "1.0.5",
      "fwv": "1.0.1",
      "hwv": "v01.3",
      "swv": "b00.2",
      "prototype": "1",
      "prodId": "000b",
    },
    "services": [
      {
        "sid": "keyEvent",
        "st": "keyEvent",
        "ts": "20171225T074217Z",
        "data": {
          "key": 0
        }
      },
      …
    ]
  }

  */
}

function setDevInfoAllListener(listener) {
  devInfoAllListener = listener;
}

/**
 * 获取当前设备所有缓存的信息(离线)
 */
function getDevCache() {
  if (window.hilink) {
    hilink.getDevCache(
      "0", //devId(String): 华为云分配给设备的唯一id,获取本设备时传0。
      "", //body(String): 该参数为预留参数，当前请传空值。
      "getDevCacheCallback" //resultCallback，回调方法，成功或失败时，将调用传入resultStr返回结果
    );
  }
}
window.getDevCacheCallback = res => {
  let data = dataChange(res);
  if (devCacheListener != null) {
    devCacheListener(data);
  }
  /*
  {
    "devId": "479******************************443",
    "gatewayId": "a67******************************ec2",
    "nodeType": "ENDPOINT",
    "devInfo": {
      "sn": "@LH**************168",
      "manu": "021",
      "devType": "038",
      "model": "B910ZB",
      "mac": "00:***********:68",
      "hiv": "1.0.5",
      "fwv": "1.0.1",
      "hwv": "v01.3",
      "swv": "b00.2",
      "prototype": "1",
      "prodId": "000b",
    },
    "services": [
      {
        "sid": "keyEvent",
        "st": "keyEvent",
        "ts": "20171225T074217Z",
        "data": {
          "key": 0
        }
      },
      …
    ]
  }

  */
}

function setDevCacheListener(listener) {
  devCacheListener = listener;
}


/**
 * 手机ap从后台恢复时，自动调用该函数
 */
window.onResume = () => {
  console.log('APP从后台恢复到设备页了');
  if (onResumeListener != null) {
    onResumeListener();
  }
}

function setOnResumeListener(listener) {
  onResumeListener = listener;
}

/**
 * 手机从前台退到后台，会自动调用该函数
 */
window.onPause = () => {
  console.log("APP从前台退到后台");
  if (onPauseListener != null) {
    onPauseListener();
  }
}

function setOnPauseListener(listener) {
  onPauseListener = listener;
}

//创建联动规则
function createRule(jsObject) {
  let json_data = JSON.stringify(jsObject); //将js对象转换成json字符串
  if (window.hilink) {
    hilink.createRule(
      json_data, //body,string, 规则json字符串
      "createRuleInfoCallback" //resultCallback，成功或失败时，将调用传入resultStr返回结果
    );
  }
}
window.createRuleInfoCallback = res => {
  let data = dataChange(res);
  if (createRuleListener != null) {
    createRuleListener(data);
  }
}

function setCreateRuleListener(listener) {
  createRuleListener = listener;
}

//删除联动规则
function deleteRule(jsObject) {
  let json_data = JSON.stringify(jsObject); //将js对象转换成json字符串

  if (window.hilink) {
    hilink.deleteRules(
      json_data, //需要删除的规则的ruleId数组的json字符串
      "deleteRuleInfoCallback" //resultCallback，成功或失败时，将调用传入resultStr返回结果
    );
  }
}
window.deleteRuleInfoCallback = res => {
  toast(1200, res);
  let data = dataChange(res);

  if (deleteRuleListener != null) {
    deleteRuleListener(data);
  }
}

function setDeleteRuleListener(listener) {
  deleteRuleListener = listener;
}

//获取所有联动规则
/**
 *
 * @param {string}} jsObject 查询当前家庭下所有规则的过滤条件
 * ruleId string	规则Id
 * executor String根据执行主体查询
 * ruleType int	只支持1/2，不传递或者传递错误，默认为1。根据类型过滤，app的单品页面可以查询规则类型为2的规则，app的ifttt页面查询类型为1的规则，如果没有传递或者传递错误，默认为查询类型为1的规则
 * name	String	规则名称,创建时候保证了不冲突
 */
function getRules(jsObject) {
  let json_data = JSON.stringify(jsObject); //将js对象转换成json字符串
  if (window.hilink) {
    hilink.getRules(
      json_data, //body,string, 查询当前家庭下所有规则的过滤条件
      "getRulesInfoCallback" //resultCallback，成功或失败时，将调用传入resultStr返回结果
    );
  }
}
window.getRulesInfoCallback = res => {
  let data = dataChange(res);
  if (getRulesListener != null) {
    getRulesListener(data);
  }
}

function setGetRuleListener(listener) {
  getRulesListener = listener;
}


function controlRuleActive(ruleId, active) {

  hilink.controlRuleActive(
    ruleId, //string, 需要控制的规则的id
    active, //string, 规则的激活状态，"active"为激活，"inactive"为未激活
    "controlRuleActiveCallback" //resultCallback，成功或失败时，将调用传入resultStr返回结果
  );
}
window.controlRuleActiveCallback = res => {
  let data = dataChange(res);
  if (controlRuleActiveListener != null) {
    controlRuleActiveListener(data);
  }
}

function setControlRuleActiveListener(listener) {
  controlRuleActiveListener = listener;
}

//跳转到默认的设置页面
function jumpToSetting() {
  if (window.hilink) {
    hilink.jumpTo(
      "com.huawei.smarthome.deviceSettingActivity",
      "jumpToSettingCallback"
    );
  }
}
window.jumpToSettingCallback = res => {
  let data = dataChange(res);
  if (jumpToSettingListener != null) {
    jumpToSettingListener(data);
  }
}

function setJumpToSettingListener(listener) {
  jumpToSettingListener = listener;
}

//判断折叠屏是否展开
function isScreenSpreaded() {
  if (window.hilink) {
    return hilink.isScreenSpreaded();
  }
  return false;
}

//向数据库中存储键值对
function saveStorageSync(key, value) {
  if (window.hilink) {
    hilink.setStorageSync(
      key, //string，往数据库中存储的键值
      value, //string，往数据库中存储字段对应的值
      "saveStorageSyncCallback" //resultCallback，成功或失败时，将调用传入resultStr返回结果
    );
  }
}
window.saveStorageSyncCallback = res => {
  let data = dataChange(res);
  if (saveStorageSyncListener != null) {
    saveStorageSyncListener(data);
  }
}

function setSaveStorageSyncListener(listener) {
  saveStorageSyncListener = listener;
}

//从数据库中获取键值对
function getStorageSync(key) {
  if (window.hilink) {
    hilink.getStorageSync(
      key, //string，往数据库中存储的键值
      "getStorageSyncCallback" //resultCallback，成功或失败时，将调用传入resultStr返回结果
    );
  }
}
window.getStorageSyncCallback = res => {
  let data = dataChange(res);
  if (getStorageSyncListener != null) {
    getStorageSyncListener(data);
  }
}

function setGetStorageSyncListener(listener) {
  getStorageSyncListener = listener;
}

//从数据库中删除键值对
function removeStorageSync(key) {
  if (window.hilink) {
    hilink.removeStorageSync(
      key, //string，往数据库中存储的键值
      "removeStorageSyncCallback" //resultCallback，成功或失败时，将调用传入resultStr返回结果
    );
  }
}
window.removeStorageSyncCallback = res => {
  let data = dataChange(res);
  if (removeStorageSyncListener != null) {
    removeStorageSyncListener(data);
  }
}

function setRemoveStorageSyncListener(listener) {
  removeStorageSyncListener = listener;
}

//是否是装维App
function getAiInstall() {
  if (window.hilink) {
    return hilink.isAiInstall();
  }
  return false;
}

function getCurrentTime() {
  let date = new Date();
  let yy = date.getFullYear();
  let mm = date.getMonth() + 1;
  let dd = date.getDate();
  let hh = date.getHours();
  let mf = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes();
  let ss = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
  let dt = date.getTime().toString();
  let ms = dt.substring(dt.length - 3);
  return '[' + yy + '/' + mm + '/' + dd + ' ' + hh + ':' + mf + ':' + ss + ':' + ms + ']';
}




export {
  refreshWebView,
  modifyTitleBar,
  setChangeTitleStyle,
  overrideTitleBarRightBtn,
  setgoSettingListener,
  setMessageVisible,
  getStatusBarHeight,
  setStatuBarHeightListener,
  sendProperty,
  setPropertiesListener,
  sendProperties,
  setNavigationVisible,
  getDevCacheAll,
  setDevCacheAllListener,
  getProperty,
  toast,
  debugLog,
  userLog,
  getDarkMode,
  finishDeviceActivity,
  deleteDevice,
  modifyDeviceName,
  modifyDeviceNameById,
  modifyRoomName,
  getDeviceInfo,
  setDeviceInfoListener,
  getDevInfoAll,
  setDevInfoAllListener,
  getDevCache,
  setDevCacheListener,
  setDeleteDeviceCallBack,
  setModifyDeviceNameCallBack,
  setModifyDeviceNameByIdCallBack,
  setModifyRoomCallBack,
  setSendPropertyListener,
  setSendPropertiesListener,
  setRequestPropertyListener,
  setOnResumeListener,
  setOnPauseListener,

  createRule,
  setCreateRuleListener,
  deleteRule,
  setDeleteRuleListener,
  getRules,
  setGetRuleListener,
  controlRuleActive,
  setControlRuleActiveListener,
  jumpToSetting,
  setJumpToSettingListener,

  getSwitchProperties,
  setRequestSwitchPropertiesListener,
  getControlProperties,
  setRequestControlPropertiesListener,
  getStatusProperties,
  setRequestStatusPropertiesListener,
  getGearProperties,
  setRequestGearPropertiesListener,
  getWarnEventProperties,
  setRequestWarnEventPropertiesListener,
  getFaultEventProperties,
  setRequestFaultEventPropertiesListener,
  isScreenSpreaded,

  saveStorageSync,
  setSaveStorageSyncListener,
  getStorageSync,
  setGetStorageSyncListener,
  removeStorageSync,
  setRemoveStorageSyncListener,

  getCurrentTime,

  getAppLanguage,

  sendPropertyWithCallback,
  dataChange,

  getAiInstall,
}
